package dd.prog2.musterExercises.ws12set11_cnt_gui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * Resettable counter application, realized with local classes.
 * @author Ruediger Lunde
 */
public class CounterApp4 {

	private JTextField textField;
	private int value;
	private JButton countButton;
	private JButton resetButton;
	private JButton exitButton;

	/** Starts the counter application. */
	public void start() {
		JFrame frame = new JFrame("Counter V4");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		textField = new JTextField();
		textField.setFont(new Font("SansSerif", Font.BOLD, 60));
		textField.setEditable(false);
		textField.setText(Integer.toString(value));
		frame.add(textField, BorderLayout.CENTER);
		JPanel buttonPanel = new JPanel();
		frame.add(buttonPanel, BorderLayout.SOUTH);

		countButton = new JButton("Count");
		buttonPanel.add(countButton);
		countButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				value++;
				textField.setText(Integer.toString(value));
			}
		});
		resetButton = new JButton("Reset");
		buttonPanel.add(resetButton);
		resetButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				value = 0;
				textField.setText(Integer.toString(value));
			}
		});
		exitButton = new JButton("Exit");
		buttonPanel.add(exitButton);
		exitButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		
		frame.pack(); // adjust frame size, so that all elements are happy!
		frame.setVisible(true);
	}

	/** Creates the counter application and starts it. */
	public static void main(String[] args) {
		new CounterApp4().start();
	}
}
